package com.puzzlebazar.client.main.model;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.puzzlebazar.client.main.model.PuzzleListInfoFactory.PuzzleListInfoCreator;
import com.puzzlebazar.client.puzzleinfo.PuzzleInfo;
import com.puzzlebazar.client.services.PuzzleServiceAsync;
import com.puzzlebazar.client.services.ServiceFactory;
import com.puzzlebazar.client.user.UserInfo;
import com.puzzlebazar.client.util.Capabilities;

public class PuzzleListInfoSimple extends PuzzleListInfoImpl {

	/**
	 * Register the various simple puzzle list information structures
	 * 
	 * @param factory The factory to register with
	 */
	public static void register( PuzzleListInfoFactory factory ) {
		for (final ListType type : ListType.values() ) {
			factory.registerPuzzleListInfo( type.toString(), new PuzzleListInfoCreator() {
				@Override
				public PuzzleListInfo createPuzzleListInfo(
						HashMap<String, String> params) {
					return getInstance(type);
				}} );
		}
	}
	
	private static EnumMap<ListType,PuzzleListInfoSimple> instances 
		= new EnumMap<ListType,PuzzleListInfoSimple>(ListType.class);
	
	/**
	 * Access a specific instance of a PuzzleListInfoSimple
	 * 
	 * @param listType The type of list information to access
	 * @return The instance of puzzle list information for that type
	 */
	public static PuzzleListInfoSimple getInstance( ListType listType ) {
		PuzzleListInfoSimple instance = instances.get( listType );
		if( instance == null ) {
			instance = new PuzzleListInfoSimple(listType);
			instances.put( listType, instance );
		}
		return instance;			
	}
	
	private final ListType listType;
	
	
	/**
	 * Creates a puzzle list information class with a single unique list type
	 * 
	 * @param listType The list type
	 */
	protected PuzzleListInfoSimple( ListType listType ) {
		super( listType.toString() );
		this.listType = listType;
	}
	
	@Override
	public void callService(AsyncCallback<List<PuzzleInfo>> callback) {
		PuzzleServiceAsync puzzleService = ServiceFactory.getPuzzleService(); 		
		puzzleService.getPuzzleList(listType, null, callback );
	}

	@Override
	public String getName() {
		switch( listType ) {
		case ALL_PUZZLES:
			return "All Puzzles";			
		case PLAYABLE_PUZZLES:
			return "Recent Puzzles";
		case EDITABLE_PUZZLES:
			return "Drafts";
		case VIEWABLE_TUTORIALS:
			return "Recent Tutorials";
		case EDITABLE_TUTORIALS:
			return "Draft Tutorials";
		}
		
		return null;
	}

	@Override
	public boolean canUserGetList(UserInfo userInfo) {
		switch( listType ) {
		case ALL_PUZZLES:
			return Capabilities.canListAllPuzzles( userInfo );
		case PLAYABLE_PUZZLES:
			return Capabilities.canListPlayablePuzzles( userInfo );
		case EDITABLE_PUZZLES:
			return Capabilities.canListEditablePuzzles( userInfo );
		case VIEWABLE_TUTORIALS:
			return Capabilities.canListViewableTutorials( userInfo );
		case EDITABLE_TUTORIALS:
			return Capabilities.canListEditableTutorials( userInfo );
		}
		
		return true;
	}



		
}
